package com.sds.service.impl;

import com.sds.service.WaitSleepService;
import org.springframework.stereotype.Service;

@Service
public class WaitSleepServiceImpl implements WaitSleepService {

    private final Object lock = new Object();

    @Override
    public void demonstrateSleep() {
        // 创建一个新线程
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                try {
                    // 让线程睡眠1秒
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        // 启动线程
        t1.start();
    }

    @Override
    public void demonstrateWait() {
        // 创建一个新线程
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                try {
                    // 让线程等待
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        // 启动线程
        t1.start();
    }
}